<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY css SYSTEM "../phaidros.css">
]>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:include href="popup.xsl"/>
<xsl:include href="topbuttons.xsl"/>

<xsl:template match="/">
<xsl:variable name="versionId" select="//response/content/@version"/>
<xsl:variable name="file" select="//response/@file"/>
<xsl:variable name="longName" select="//version[number($versionId)]/@longName"/>
<xsl:variable name="groupId" select="//version[number($versionId)]/@group"/>
<xsl:variable name="groupName" select="//group[number($groupId)]/@name"/>
<html>
<head>
<title><xsl:value-of select="//teiHeader/fileDesc/titleStmt/title"/></title>
<style type="text/css">
&css;
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
<xsl:text disable-output-escaping="yes">
function popup()
{
    setVersion();
    document.submission.submit();
}
function setVersion( id, rawLink )
{
	var menu = document.getElementById("versionPopup");
    var version = document.getElementById("Version");
    var parts = menu.options[menu.selectedIndex].id.split(".");
    version.value = parts[1];
}
function revert()
{
	// this will revert because it will 
	// reread the version without saving it
	setVersion();
	var com = document.getElementById("Command");
	com.value = "Revert";
	document.submission.submit();
}
function redirect( action )
{
	setVersion();
	document.submission.action=action;
	document.submission.submit();
}
function save()
{
	if ( xmlIsWellFormed() )
	{
		setVersion();
		var content = document.getElementById("Content");
		var textarea = document.getElementById("EditField");
    	content.value = textarea.value;
		var com = document.getElementById("Command");
		com.value = "Save";
		//alert("Document is well-formed");
    	document.submission.submit();
    }
}
function xmlIsWellFormed()
{
	var textarea = document.getElementById("EditField");
	var text = textarea.value;
	var result = parseElement( text, 0 );
	if ( result &lt; 0 )
	{
		var realOffset = -result;
		var end = 20 - result;
		var start = end - 40;
		if ( end &gt; text.length-1 )
			end = text.length-1;
		if ( start &lt; 0 )
			start = 0;
		alert( "XML is not well-formed at offset "
			+realOffset
			+" near '"
			+text.substring(start,end)
			+"'" );
		return false;
	}
	else
		return true;
}
function parseElement( text, index )
{
	var index = readStartTag( text, index );
	if ( index &gt; 0 )
		index = readBody( text, index );
	if ( index &gt; 0 )
		index = readEndTag( text, index );
	return index;
}
function readStartTag( text, index )
{
	var len = text.length;
	// point to first non-white space
	while ( index &lt; len &amp;&amp; 
		isWhiteSpace(text.charCodeAt(index)) )
		index++;
	// look for compulsory left angle bracket
	if ( text.charCodeAt(index) != 60 )
	{
		return -index;
	}
	else
		index++;
	// check there is no end-tag slash
	if ( text.charCodeAt(index) == 47 )
	{
		return -index;
	}
	// look for right angle bracket
	while ( index &lt; len &amp;&amp; 
		text.charCodeAt(index) != 62 )
	{
		if ( text.charCodeAt(index)==60 )
			return -index;
		else
			index++;
	}
	// check it is not an empty element
	if ( text.charCodeAt(index-1) == 47 )
	// leave it pointing to the forward slash
		index--;
	else
		index++;
	return index;
}
function readBody( text, index )
{
	var len = text.length;
	// check for empty body
	if ( text.charCodeAt(index)!= 47 
		|| (index &lt; len-1 
		&amp;&amp; text.charCodeAt(index+1)!=62) )
	{
		while ( index &gt;= 0 )
		{
			while ( index &lt; len &amp;&amp; 
				text.charCodeAt(index) != 60 )
				index++;
			if ( index &lt; len-1 &amp;&amp; text.charCodeAt(index+1) != 47 )
				index = parseElement( text, index );
			else
				break;
		}
	}
	return index;
}
function readEndTag( text, index )
{
	var len = text.length;
	if ( text.charCodeAt(index)== 47 
		&amp;&amp; (index &lt; len-1) 
		&amp;&amp; text.charCodeAt(index+1)==62 )
		return index+2;
	else if ( text.charCodeAt(index)==60 
		&amp;&amp; (index &lt; len-1) 
		&amp;&amp; text.charCodeAt(index+1)==47 )
	{
		while ( index &lt; len &amp;&amp; text.charCodeAt(index)!=62 )
			index++;
		return index+1;
	}
	else
		return -index;
}
function isWhiteSpace( code )
{
 	return code == 32 
 		|| code == 10 
 		|| code == 13
 		|| code == 9;
}
function notImplemented()
{
	alert("Sorry, this feature is not yet implemented");
}
// dynamically resize displayBox div
function resize()
{
	var editField = document.getElementById("EditField");
	var div = document.getElementById("editBox");
	var divInfo = document.getElementById("infoBox");
	var divHeader = document.getElementById("header");
	var height = window.innerHeight - (divInfo.offsetHeight
		+divHeader.offsetHeight);
	div.style.height=height+"px";
	height -= 50;
	editField.style.height = height+"px";
}
</xsl:text>
</script>
</head>
<body onload="resize()">
<div id="header" class="header">
<xsl:call-template name="writeTopButtons">
	<xsl:with-param name="writeSave">1</xsl:with-param>
	<xsl:with-param name="disabled">EditSingleViewButton</xsl:with-param>
</xsl:call-template>
</div>

<div class="centralColumn">
<form name="submission" action="/Alpha/EditView" method="post">

<!-- version popup, description and hidden fields -->
<div id="infoBox" class="infoBox">
<xsl:call-template name="popup">
	<xsl:with-param name="versionId" select="$versionId"/>
	<xsl:with-param name="popupName">versionPopup</xsl:with-param>
</xsl:call-template><xsl:text> </xsl:text>
<span id="versionField" class="information"><xsl:value-of select="$longName"/></span>
<!-- submission fields -->
<input id="Version" name="Version1" type="hidden"/>
<input id="Content" name="Content" type="hidden"/>
<input id="File" name="File" type="hidden"><xsl:attribute name="value">
<xsl:value-of select="$file"/></xsl:attribute></input>
<input id="Command" name="Command" type="hidden"/>
</div>

<!-- main editing field-->
<div id="editBox" class="editBox">
<textarea class="editfield" id="EditField"><xsl:copy-of select="//TEI.2 |//TEI"/>
</textarea>
</div>
</form>
<!-- end centralColumn-->
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>